import LayVmodelBase from "./LayVmodelBase.js"
import { logger, tools, runtime } from "./tools.js";


/**Layui 输入框 */
class LayInputVmodel extends LayVmodelBase {
    valueEvent = "change";
    constructor(elem) {
        super(elem);
    }

    /**[override] */
    viewModelInit() {
        super.viewModelInit();
        this.vm.value = ko.observable("");
    }

    /**[override] */
    init() {
        if (!super.init()) return;
        var tis = this;
        //订阅value变化
        this.vm.value.subscribe((newValue) => {
            $(this.element).val(newValue);
        });
        //订阅value变化
        $(this.element).on(tis.valueEvent, function () {
            tis.vm.value($(this).val());
        });
        //layui 2.8.10 数字输入框
        if ($(this.element).attr("lay-affix") === "number") {
            let form = tools.laymod('form');
            form.on('input-affix', function (data) {
                var elem = data.elem; // 获取输入框 DOM 对象
                if (elem !== tis.element) return;
                var affix = data.affix; // 获取输入框 lay-affix 属性值
                let value = $(elem).val();
                tis.vm.value(value);
            });
        }

        $(this.element).val(this.vm.value());
        this.inited = true;
    }
}

export default LayInputVmodel;